package com.gl.order.config;

//import org.springframework.amqp.core.TopicExchange;


import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * rabbitmq配置
 *
 * @author liugh
 *
 */

@Configuration
public class RabbitmqConfig {
    //配置mq(roadtoll)
    @Bean(name="roadtollConnectionFactory")
    @Primary
    public ConnectionFactory roadtollConnectionFactory(
            @Value("${spring.rabbitmq.host}") String host,
            @Value("${spring.rabbitmq.port}") int port,
            @Value("${spring.rabbitmq.username}") String username,
            @Value("${spring.rabbitmq.password}") String password,
            @Value("${spring.rabbitmq.virtual-host}") String virtualHost){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean(name="roadtollRabbitTemplate")
    @Primary
    public RabbitTemplate firstRabbitTemplate(
            @Qualifier("roadtollConnectionFactory") ConnectionFactory connectionFactory
    ){
        RabbitTemplate roadtollRabbitTemplate = new RabbitTemplate(connectionFactory);
        //使用外部事物
        //ydtRabbitTemplate.setChannelTransacted(true);
        return roadtollRabbitTemplate;
    }



    @Bean(name="roadtollFactory")
    public SimpleRabbitListenerContainerFactory roadtollCFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("roadtollConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }


}